﻿using System.Text;
using System;
using System.Collections.Generic;

namespace Core
{
    public static partial class Utils
    {
        public static class Text
        {
            [ThreadStatic] private static StringBuilder sCachedStringBuilder;

            public static string Format(string format, object arg0)
            {
                if (format == null)
                {
                    throw new Exception("Format is invalid.");
                }

                CheckCachedStringBuilder();
                sCachedStringBuilder.Length = 0;
                sCachedStringBuilder.AppendFormat(format, arg0);
                return sCachedStringBuilder.ToString();
            }

            public static string Format(string format, object arg0, object arg1)
            {
                if (format == null)
                {
                    throw new Exception("Format is invalid.");
                }

                CheckCachedStringBuilder();
                sCachedStringBuilder.Length = 0;
                sCachedStringBuilder.AppendFormat(format, arg0, arg1);
                return sCachedStringBuilder.ToString();
            }

            public static string Format(string format, object arg0, object arg1, object arg2)
            {
                if (format == null)
                {
                    throw new Exception("Format is invalid.");
                }

                CheckCachedStringBuilder();
                sCachedStringBuilder.Length = 0;
                sCachedStringBuilder.AppendFormat(format, arg0, arg1, arg2);
                return sCachedStringBuilder.ToString();
            }

            public static string Format(string format, params object[] args)
            {
                if (format == null)
                {
                    throw new Exception("Format is invalid.");
                }

                if (args == null)
                {
                    throw new Exception("Args is invalid.");
                }

                CheckCachedStringBuilder();
                sCachedStringBuilder.Length = 0;
                sCachedStringBuilder.AppendFormat(format, args);
                return sCachedStringBuilder.ToString();
            }

            private static void CheckCachedStringBuilder()
            {
                sCachedStringBuilder ??= new StringBuilder(1024);
            }

            public static string GetSizeFormat(long size)
            {
                string arg;
                float num;
                if (size < 1024)
                {
                    arg = "B";
                    num = size;
                }
                else if (size < 1024 * 1024)
                {
                    arg = "KB";
                    num = size / 1024f;
                }
                else
                {
                    arg = "MB";
                    num = size * 1.0f / 1024 * 1024;
                }

                return $"{num:F2}{arg}";
            }

            public static string ToUpper(string s, int index)
            {
                if (index < 0)
                {
                    return s;
                }

                if (index >= s.Length)
                {
                    return s.ToUpper();
                }

                return s[..index].ToUpper() + s[index..];
            }

            public static string ToLower(string s, int index)
            {
                if (index < 0)
                {
                    return s;
                }

                if (index >= s.Length)
                {
                    return s.ToLower();
                }

                return s[..index].ToLower() + s[index..];
            }

            private static readonly string[,] Roman = new string[,]
            {
                { "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" },
                { "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" },
                { "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" },
                { "", "M", "MM", "MMM", "", "", "", "", "", "" }
            };

            /// <summary>
            /// 输入一个正的个位数整数，输出罗马字符
            /// </summary>
            /// <param name="n"></param>
            /// <returns></returns>
            public static string ToRoman(int n)
            {
                if (n > 3999)
                {
                    log.d($"暂无法表示超过3999的罗马字符");
                    return "";
                }

                string value = string.Empty;
                value += Roman[3, n / 1000 % 10];
                value += Roman[2, n / 100 % 10];
                value += Roman[1, n / 10 % 10];
                value += Roman[0, n % 10];
                return value;
            }

            public static string FormatCurrency(int value)
            {
                string str;
                switch (value)
                {
                    case < 10000:
                        str = value.ToString();
                        break;
                    case < 100000000:
                        str = (value / 10000f).ToString("0.0");
                        str = $"{str}万";
                        break;
                    //case < 1000000000:
                    //    str = (value / 1000000f).ToString("0.0");
                    //    str = $"{str}m";
                    //    break;
                    default:
                        str = (value / 100000000f).ToString("0.0");
                        str = $"{str}亿";
                        break;
                }

                return str;
            }

            public static List<int> ToIntList(string str)
            {
                var list = new List<int>();

                if (string.IsNullOrWhiteSpace(str)) return list;

                var strArr = str.Split(",");

                for (var i = 0; i < strArr.Length; i++)
                {
                    if (int.TryParse(strArr[i], out var value))
                    {
                        list.Add(value);
                    }
                }

                return list;
            }
        }
    }
}
